package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.OutboundCheck;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IOutboundService;
import com.woniu.supermarket.util.CodeCreateUtil;
import com.woniu.supermarket.vo.OutboundVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("Outbound")
@Api(value = "出库单操作")
public class OutboundController {

    @Autowired
    IOutboundService iOutboundService;


    @GetMapping("/showOutbound")
    @ApiOperation(value = "出库单分页查询操作")
    public ResultResponse<PageInfo<OutboundVo>> showOutbound(@ApiParam(name = "now",value = "当前页")@RequestParam(name = "now",defaultValue = "1") Integer now,
                                                             @ApiParam(name = "size",value = "页大小")@RequestParam(name = "size",defaultValue = "3") Integer size,
                                                             @ApiParam(name = "outboundNumber",value = "出库单号") String outboundNumber,
                                                             @ApiParam(name = "status",value = "状态")@RequestParam(name = "status") Integer status){
        ResultResponse<PageInfo<OutboundVo>> result = null;
        System.out.println(outboundNumber);
        try {
            PageInfo<OutboundVo> pageInfo = iOutboundService.findOutboundByCons(now, size, outboundNumber, status);
            result = new ResultResponse<>(200,"查询出库单成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询出库单失败");
        }
        return result;
    }



    @PutMapping("/modifyOutbound")
    @ApiOperation(value = "出库修改操作")
    public ResultResponse<Void> modifyOutbound(@RequestBody OutboundVo outboundVo){
        ResultResponse<Void> result = null;
        System.out.println(outboundVo.getOutboundTime());
        try {
            iOutboundService.modifyOutbound(outboundVo);
            result = new ResultResponse<>(200,"修改出库单成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改出库单失败");
        }
        return result;
    }


    @PutMapping("/modifyOutbound2")
    @ApiOperation(value = "出库修改操作")
    public ResultResponse<Void> modifyOutbound2(@RequestBody OutboundVo outboundVo){
        ResultResponse<Void> result = null;
        System.out.println(outboundVo.getOutboundTime());
        try {
            iOutboundService.modifyOutbound(outboundVo);
            result = new ResultResponse<>(200,"修改出库单成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改出库单失败");
        }
        return result;
    }




    @PostMapping("/addOutbound")
    @ApiOperation(value = "商品出库单添加操作")
    public ResultResponse<Void> addOutbound(@RequestBody OutboundVo outboundVo){
        System.out.println(outboundVo.getOutboundDetailList());
        ResultResponse<Void> result = null;
        System.out.println(outboundVo.getOutboundTime());
        try {
            iOutboundService.addOutbound(outboundVo);
            result = new ResultResponse<>(200,"添加出库单成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加出库单失败");
        }
        return result;
    }



    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "出库修改操作")
    public ResultResponse<Void> del(@PathVariable int id){
        ResultResponse<Void> result = null;
        try {
            iOutboundService.removeOutbound(id);
            result = new ResultResponse<>(200,"删除出库单成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除出库单失败");
        }
        return result;
    }



    @GetMapping("/OutboundCode")
    @ApiOperation(value = "创建出库编码")
    public ResultResponse<String> OutboundCode(){
        ResultResponse<String> result = null;
        try {
            String code = CodeCreateUtil.createOutboundCode();
            result = new ResultResponse<>(200,"创建出库编码成功",code);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"创建出库编码失败");
        }
        return result;
    }




    @PostMapping("/auditOutbound")
    @ApiOperation(value = "添加审核操作")
    public ResultResponse<OutboundCheck> auditOutbound(@RequestBody OutboundCheck outboundCheck){
        ResultResponse<OutboundCheck> result = null;
        try {
            iOutboundService.addOutboundCheck(outboundCheck);
            result = new ResultResponse<>(200,"审核出库订单成功",outboundCheck);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"审核出库订单失败");
        }
        return result;
    }
}
